<?php

namespace app\common\model;

use think\Exception;

class PyCity extends BaseModel
{
    //添加城市
    public function add($data){
        //判断城市坐标是否选择
        if($data['cty_longitude']+$data['cty_latitude']+$data['cty_zoom']==0){
            return "请选择城市地址";
        }

        //判断城市名是否存在
        $cty_name=$data['cty_name'];
        $isset=$this->where("cty_name","=",$cty_name)->find();
        if($isset){
            return "该城市名已存在";
        }


        //数据插入
        $data=$this->dataFilter($data);
        try{
            $this->insert($data);
        }catch (Exception $exception){
            return "数据插入异常";
        }

        return 1;

    }

    //修改城市信息
    public function edit($data,$cty_id){
        $cty_name=$data['cty_name'];
        $issetCondition=[];
        $issetCondition['cty_name']=['eq',$cty_name];
        $issetCondition['cty_id']=['neq',$cty_id];
        $isset=$this->where($issetCondition)->find();
        if($isset){
            return "该城市名已存在";
        }

        //修改数据
        $data=$this->dataFilter($data);
        try{
            $result=$this->where("cty_id","=",$cty_id)->update($data);
        }catch (Exception $exception){
            return "数据编辑异常";
        }

        if(!$result){
            return "数据未发生变化";
        }
        return 1;

    }
}
